package com.sth.staservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sth.commonutils.R;
import com.sth.commonutils.UUIDUtil;
import com.sth.staservice.client.Uncenter;
import com.sth.staservice.entity.StatisticsDaily;
import com.sth.staservice.mapper.StatisticsDailyMapper;
import com.sth.staservice.service.StatisticsDailyService;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author sth
 * @since 2020-12-26
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {
    @Autowired
    private Uncenter uncenter;

    @Override
    public void registerCount(String day) {

        // 添加记录之前删除相同日期的数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated", day);
        baseMapper.delete(wrapper);
        // 远程调用
        R r = uncenter.countRegister(day);
        Integer countRegister = (Integer) r.getData().get("countRegister");
        // 把获取到的数据,添加到数据库中
        StatisticsDaily sta = new StatisticsDaily();
        sta.setId(UUIDUtil.get16UUID());
        sta.setRegisterNum(countRegister);
        sta.setDateCalculated(day);
        sta.setVideoViewNum(RandomUtils.nextInt(100, 200));
        sta.setCourseNum(RandomUtils.nextInt(100, 200));
        sta.setLoginNum(RandomUtils.nextInt(100, 200));
        baseMapper.insert(sta);
    }

    // 图表显示 .返回两部分的数据 ,日期json,数量json 数组
    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        // 根据条件查询对应的数据
        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("date_calculated", begin, end);
        queryWrapper.select("date_calculated", type);
        List<StatisticsDaily> stalist = baseMapper.selectList(queryWrapper);
        // 两部分数据 日期, 和日期对应的数量
        // 日期list, 数量list
        // 日期list, 数量list
        List<String> dateDataList = new ArrayList<>();
        List<Integer> numDataList = new ArrayList<>();
        stalist.stream().forEach(item -> {
            dateDataList.add(item.getDateCalculated());
            switch (type) {
                case "login_num":
                    numDataList.add(Optional.ofNullable(item.getLoginNum()).orElse(0));
                    break;
                case "register_num":
                    numDataList.add(item.getRegisterNum());
                    break;
                case "video_view_num":
                    numDataList.add(item.getVideoViewNum());
                case "course_num":
                    numDataList.add(item.getCourseNum());
                    break;
                default:
                    break;
            }
        });
        Map<String, Object> map = new HashMap<>();
        map.put("date_calculatedList", dateDataList);
        map.put("numDataList", numDataList);
        return map;
    }
}
